module test;

import SimpleSDL.main;
import SimpleSDL.sdl;
import derelict.sdl.sdl;

import tango.io.Stdout;

void main ()
{
    initSDL ();

    // Create an main window which size is 1024x768.
    auto display = new SDLDisplay (1024,768);

    // Set title of main window
    display.setTitle ("Hello SimpleSDL");

    // Create an background SDL Surface from a image file.
    auto background = new SDLSurface ("images/background.jpg");

    // Create an foreground SDL Surface from a image file.
    auto foreground = new SDLSurface ("images/foreground.png");

    // Create layer 1 by background surface
    auto layer1 = display.addLayer (background, "background");

    // Create layer 1 by foreground surface at (300, 200) of main window.
    auto layer2 = display.addLayer (foreground, "foreground", 300, 200);

    // Set foreground to half transparent
    layer2.alpha = 127;

    // Update main window.
    display.render ();

    // The following is same with SDL C Library.
    bool running = true;
    SDL_Event  event;
    while (running) {
        SDL_WaitEvent (&event);
        bool shouldUpdate = false;

        switch (event.type) {
            // When mouse moved, move layer2.
            case SDL_MOUSEMOTION:
                int x = event.motion.x;
                int y = event.motion.y;
                
                layer2.move (x - layer2.width/2, y - layer2.height/2);
                shouldUpdate = true;
                break;

            // When mouse button is pressed, toggle
            // layer2.
            case SDL_MOUSEBUTTONDOWN:
               
                switch (layer2.hide) {
                    case true:
                        layer2.fadeIn ();

                        layer2.moveX (-100, 5);
                        layer2.moveY (-100, 5);
                        layer2.moveX (100, 5);
                        layer2.moveY (100, 5);
                        break;

                    case false:
                        layer2.moveX (-100, 5);
                        layer2.moveY (-100, 5);
                        layer2.moveX (100, 5);
                        layer2.moveY (100, 5);

                        layer2.fadeOut ();
                        break;
                }
                break;

            // When close main window, exit the program.
            case SDL_QUIT:
                running = false;
                break;

            default:
        }

        // Update main window when needed.
        if (shouldUpdate) {
            display.render ();
        }
    }
}
